<?php

/**
 * news actions.
 *
 * @package    explications_informatiques
 * @subpackage news
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class newsActions extends sfActions
{


  public function executeNew(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_NEWS"),"headers","forbidden");
    $this->form = new NewsForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_NEWS"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");

    $this->form = new NewsForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MOD_NEWS"),"headers","forbidden");
    $this->forward404Unless($news = Doctrine_Core::getTable('News')->find(array($request->getParameter('idnews'))), sprintf('Object news does not exist (%s).', $request->getParameter('idnews')));
    $this->form = new NewsForm($news);
  }

  public function executeUpdate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MOD_NEWS"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT),"headers","forbidden");
    $this->forward404Unless($news = Doctrine_Core::getTable('News')->find(array($request->getParameter('idnews'))), sprintf('Object news does not exist (%s).', $request->getParameter('idnews')));
    $this->form = new NewsForm($news);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {

	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("SUPPR_NEWS"),"headers","forbidden");
    $this->forward404Unless($news = Doctrine_Core::getTable('News')->find(array($request->getParameter('idnews'))), sprintf('Object news does not exist (%s).', $request->getParameter('idnews')));
    $news->delete();
	if ($cache = $this->getContext()->getViewCacheManager())
	{
	   $cache->remove('main/index');
	   $cache->remove('headers/modepanel');
	}
	sfCoreAutoload::logElem("Suppresion d'une news id:".$request->getParameter('idnews'), $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
	$this->getUser()->setFlash("info","News supprimé");
    $this->redirect('news/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && ($this->getUser()->hasCredential("ADD_NEWS") || $this->getUser()->hasCredential("MOD_NEWS")),"headers","forbidden");
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      if(!empty($request["adverts"]["id"]))
	  	$news = Doctrine_Core::getTable('news')->findOneByUserId($request["news"]["id"]);
	  else
	  	$news = new News();
	  
	  $news->setTitre($request["news"]["titre"]);
	  $news->setTexte($request["news"]["texte"]);
	  $news->setAuteur($this->getUser()->getGuardUser()->getId());
	  $news->setDateAjout(date("Ymdhis"));
	  $news->setOnline("1");
	  $news->save();
	  if ($cache = $this->getContext()->getViewCacheManager())
		{
		   $cache->remove('main/index');
		   $cache->remove('headers/modepanel');
		}
	  if($request->getParameter('action') == "create")
	  	$this->getUser()->setFlash("info","News ajoutée");
	  else
	  	$this->getUser()->setFlash("info","News modifiée");
      $this->redirect('@homepage');
    }
  }
}
